from rest_framework import status, mixins
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet

from TaskSaas.filters.issue_filter import IssueFilterSet
from TaskSaas.serializers.issue_serializer import IssuesSerializer
from utils.sorting_util import sorting_by_single_field
from web.models import Issues
from rest_framework.response import Response
from rest_framework import generics

#https://www.django-rest-framework.org/tutorial/3-class-based-views/

class IssuesList(ModelViewSet):
    queryset = Issues.objects.all()
    serializer_class = IssuesSerializer
    filterset_class = IssueFilterSet
    ordering = ["-latest_update_datetime","-create_datetime"]

    """
    List all snippets, or create a new snippet.
    """
    def list(self, request, *args,**kwargs):
        queryset = self.filter_queryset(self.get_queryset())
        queryset = sorting_by_single_field(queryset,'create_datetime,DESC',{})
        serializer = self.get_serializer(queryset, many=True)
        print("final queryset size: ",len(queryset))
        return Response(serializer.data)

    def filter_queryset(self, queryset):
        project_id = self.request.web.project.id
        print('project_id ',project_id)
        print('self.request.query_params:',self.request.query_params)
        status = self.request.query_params.getlist('status')
        print('status:',status)
        queryset = queryset.filter(project_id=project_id)
        for backend in list(self.filter_backends):
            queryset = backend().filter_queryset(self.request, queryset, self)
        return queryset



    def create(self, request, *args,**kwargs):
        return self.create(request,*args,**kwargs)
